home *** CD-ROM | disk | FTP | other *** search
/ One Click 11 / OneClick11.iso / Bancos de Dados / Conversao / Mysql2Excel / Setup.exe / Mysql2Excel.exe / win32com / server / policy.pyc (.txt) < prev   
Encoding:
Python Compiled Bytecode  |  2003-06-23  |  31.5 KB  |  698 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.2)
  3.  
  4. '''Policies 
  5.  
  6. Note that Dispatchers are now implemented in "dispatcher.py", but
  7. are still documented here.
  8.  
  9. Policies
  10.  
  11.  A policy is an object which manages the interaction between a public 
  12.  Python object, and COM .  In simple terms, the policy object is the 
  13.  object which is actually called by COM, and it invokes the requested 
  14.  method, fetches/sets the requested property, etc.  See the 
  15.  @win32com.server.policy.CreateInstance@ method for a description of
  16.  how a policy is specified or created.
  17.  
  18.  Exactly how a policy determines which underlying object method/property 
  19.  is obtained is up to the policy.  A few policies are provided, but you 
  20.  can build your own.  See each policy class for a description of how it 
  21.  implements its policy.
  22.  
  23.  There is a policy that allows the object to specify exactly which 
  24.  methods and properties will be exposed.  There is also a policy that 
  25.  will dynamically expose all Python methods and properties - even those 
  26.  added after the object has been instantiated.
  27.  
  28. Dispatchers
  29.  
  30.  A Dispatcher is a level in front of a Policy.  A dispatcher is the 
  31.  thing which actually receives the COM calls, and passes them to the 
  32.  policy object (which in turn somehow does something with the wrapped 
  33.  object).
  34.  
  35.  It is important to note that a policy does not need to have a dispatcher.
  36.  A dispatcher has the same interface as a policy, and simply steps in its 
  37.  place, delegating to the real policy.  The primary use for a Dispatcher 
  38.  is to support debugging when necessary, but without imposing overheads 
  39.  when not (ie, by not using a dispatcher at all).
  40.  
  41.  There are a few dispatchers provided - "tracing" dispatchers which simply 
  42.  prints calls and args (including a variation which uses 
  43.  win32api.OutputDebugString), and a "debugger" dispatcher, which can 
  44.  invoke the debugger when necessary.
  45.  
  46. Error Handling
  47.  
  48.  It is important to realise that the caller of these interfaces may
  49.  not be Python.  Therefore, general Python exceptions and tracebacks aren\'t 
  50.  much use.
  51.  
  52.  In general, there is an Exception class that should be raised, to allow 
  53.  the framework to extract rich COM type error information.
  54.  
  55.  The general rule is that the **only** exception returned from Python COM 
  56.  Server code should be an Exception instance.  Any other Python exception 
  57.  should be considered an implementation bug in the server (if not, it 
  58.  should be handled, and an appropriate Exception instance raised).  Any 
  59.  other exception is considered "unexpected", and a dispatcher may take 
  60.  special action (see Dispatchers above)
  61.  
  62.  Occasionally, the implementation will raise the policy.error error.  
  63.  This usually means there is a problem in the implementation that the 
  64.  Python programmer should fix.
  65.  
  66.  For example, if policy is asked to wrap an object which it can not 
  67.  support (because, eg, it does not provide _public_methods_ or _dynamic_) 
  68.  then policy.error will be raised, indicating it is a Python programmers 
  69.  problem, rather than a COM error.
  70.  
  71. '''
  72. __author__ = 'Greg Stein and Mark Hammond'
  73. import win32api
  74. import winerror
  75. import string
  76. import sys
  77. import types
  78. import win32con
  79. import pythoncom
  80. from pythoncom import DISPATCH_METHOD, DISPATCH_PROPERTYGET, DISPATCH_PROPERTYPUT, DISPATCH_PROPERTYPUTREF, DISPID_UNKNOWN, DISPID_VALUE, DISPID_PROPERTYPUT, DISPID_NEWENUM, DISPID_EVALUATE, DISPID_CONSTRUCTOR, DISPID_DESTRUCTOR, DISPID_COLLECT, DISPID_STARTENUM
  81. S_OK = 0
  82. from pywintypes import UnicodeType
  83. IDispatchType = pythoncom.TypeIIDs[pythoncom.IID_IDispatch]
  84. core_has_unicode = hasattr(__builtins__, 'unicode')
  85. from exception import COMException
  86. error = __name__ + ' error'
  87. regSpec = 'CLSID\\%s\\PythonCOM'
  88. regPolicy = 'CLSID\\%s\\PythonCOMPolicy'
  89. regDispatcher = 'CLSID\\%s\\PythonCOMDispatcher'
  90. regAddnPath = 'CLSID\\%s\\PythonCOMPath'
  91. from sys import exc_info
  92.  
  93. def CreateInstance(clsid, reqIID):
  94.     '''Create a new instance of the specified IID
  95.  
  96.   The COM framework **always** calls this function to create a new 
  97.   instance for the specified CLSID.  This function looks up the
  98.   registry for the name of a policy, creates the policy, and asks the
  99.   policy to create the specified object by calling the _CreateInstance_ method.
  100.   
  101.   Exactly how the policy creates the instance is up to the policy.  See the
  102.   specific policy documentation for more details.
  103.   '''
  104.     
  105.     try:
  106.         addnPaths = string.split(win32api.RegQueryValue(win32con.HKEY_CLASSES_ROOT, regAddnPath % clsid), ';')
  107.         for newPath in addnPaths:
  108.             if newPath not in sys.path:
  109.                 sys.path.insert(0, newPath)
  110.             
  111.     except win32api.error:
  112.         pass
  113.  
  114.     
  115.     try:
  116.         policy = win32api.RegQueryValue(win32con.HKEY_CLASSES_ROOT, regPolicy % clsid)
  117.         policy = resolve_func(policy)
  118.     except win32api.error:
  119.         policy = DefaultPolicy
  120.  
  121.     
  122.     try:
  123.         dispatcher = win32api.RegQueryValue(win32con.HKEY_CLASSES_ROOT, regDispatcher % clsid)
  124.         if dispatcher:
  125.             dispatcher = resolve_func(dispatcher)
  126.     except win32api.error:
  127.         dispatcher = None
  128.  
  129.     sys.exc_type = sys.exc_value = sys.exc_traceback = None
  130.     if dispatcher:
  131.         retObj = dispatcher(policy, None)
  132.     else:
  133.         retObj = policy(None)
  134.     return retObj._CreateInstance_(clsid, reqIID)
  135.  
  136.  
  137. class BasicWrapPolicy:
  138.     """The base class of policies.
  139.  
  140.      Normally not used directly (use a child class, instead)
  141.  
  142.      This policy assumes we are wrapping another object
  143.      as the COM server.  This supports the delegation of the core COM entry points
  144.      to either the wrapped object, or to a child class.
  145.  
  146.      This policy supports the following special attributes on the wrapped object
  147.  
  148.      _query_interface_ -- A handler which can respond to the COM 'QueryInterface' call.
  149.      _com_interfaces_ -- An optional list of IIDs which the interface will assume are
  150.          valid for the object.
  151.      _invoke_ -- A handler which can respond to the COM 'Invoke' call.  If this attribute
  152.          is not provided, then the default policy implementation is used.  If this attribute
  153.          does exist, it is responsible for providing all required functionality - ie, the
  154.          policy _invoke_ method is not invoked at all (and nor are you able to call it!)
  155.      _getidsofnames_ -- A handler which can respond to the COM 'GetIDsOfNames' call.  If this attribute
  156.          is not provided, then the default policy implementation is used.  If this attribute
  157.          does exist, it is responsible for providing all required functionality - ie, the
  158.          policy _getidsofnames_ method is not invoked at all (and nor are you able to call it!)
  159.  
  160.      IDispatchEx functionality:
  161.  
  162.      _invokeex_ -- Very similar to _invoke_, except slightly different arguments are used.
  163.          And the result is just the _real_ result (rather than the (hresult, argErr, realResult)
  164.          tuple that _invoke_ uses.\t
  165.          This is the new, prefered handler (the default _invoke_ handler simply called _invokeex_)
  166.      _getdispid_ -- Very similar to _getidsofnames_, except slightly different arguments are used,
  167.          and only 1 property at a time can be fetched (which is all we support in getidsofnames anyway!)
  168.          This is the new, prefered handler (the default _invoke_ handler simply called _invokeex_)
  169.      _getnextdispid_- uses self._name_to_dispid_ to enumerate the DISPIDs
  170.   """
  171.     
  172.     def __init__(self, object):
  173.         '''Initialise the policy object
  174.  
  175.        Params:
  176.  
  177.        object -- The object to wrap.  May be None *iff* @BasicWrapPolicy._CreateInstance_@ will be
  178.        called immediately after this to setup a brand new object
  179.     '''
  180.         if object is not None:
  181.             self._wrap_(object)
  182.         
  183.  
  184.     
  185.     def _CreateInstance_(self, clsid, reqIID):
  186.         '''Creates a new instance of a **wrapped** object
  187.  
  188.        This method looks up a "@win32com.server.policy.regSpec@" % clsid entry
  189.        in the registry (using @DefaultPolicy@)
  190.     '''
  191.         
  192.         try:
  193.             classSpec = win32api.RegQueryValue(win32con.HKEY_CLASSES_ROOT, regSpec % clsid)
  194.             myob = call_func(classSpec)
  195.             self._wrap_(myob)
  196.             return pythoncom.WrapObject(self, reqIID)
  197.         except win32api.error:
  198.             raise error, 'The object is not correctly registered - %s key can not be read' % regSpec % clsid
  199.  
  200.  
  201.     
  202.     def _wrap_(self, object):
  203.         '''Wraps up the specified object.
  204.  
  205.        This function keeps a reference to the passed
  206.        object, and may interogate it to determine how to respond to COM requests, etc.
  207.     '''
  208.         self._name_to_dispid_ = { }
  209.         ob = self._obj_ = object
  210.         if hasattr(ob, '_query_interface_'):
  211.             self._query_interface_ = ob._query_interface_
  212.         
  213.         if hasattr(ob, '_invoke_'):
  214.             self._invoke_ = ob._invoke_
  215.         
  216.         if hasattr(ob, '_invokeex_'):
  217.             self._invokeex_ = ob._invokeex_
  218.         
  219.         if hasattr(ob, '_getidsofnames_'):
  220.             self._getidsofnames_ = ob._getidsofnames_
  221.         
  222.         if hasattr(ob, '_getdispid_'):
  223.             self._getdispid_ = ob._getdispid_
  224.         
  225.         if hasattr(ob, '_com_interfaces_'):
  226.             self._com_interfaces_ = []
  227.             for i in ob._com_interfaces_:
  228.                 if type(i) != pythoncom.PyIIDType:
  229.                     if i[0] != '{':
  230.                         i = pythoncom.InterfaceNames[i]
  231.                     
  232.                 
  233.                 self._com_interfaces_.append(i)
  234.             
  235.         else:
  236.             self._com_interfaces_ = []
  237.  
  238.     
  239.     def _QueryInterface_(self, iid):
  240.         '''The main COM entry-point for QueryInterface. 
  241.  
  242.        This checks the _com_interfaces_ attribute and if the interface is not specified 
  243.        there, it calls the derived helper _query_interface_
  244.     '''
  245.         if iid in self._com_interfaces_:
  246.             return 1
  247.         
  248.         return self._query_interface_(iid)
  249.  
  250.     
  251.     def _query_interface_(self, iid):
  252.         '''Called if the object does not provide the requested interface in _com_interfaces,
  253.        and does not provide a _query_interface_ handler.
  254.  
  255.        Returns a result to the COM framework indicating the interface is not supported.
  256.     '''
  257.         return 0
  258.  
  259.     
  260.     def _Invoke_(self, dispid, lcid, wFlags, args):
  261.         '''The main COM entry-point for Invoke.  
  262.  
  263.        This calls the _invoke_ helper.
  264.     '''
  265.         if type(dispid) == type(''):
  266.             
  267.             try:
  268.                 dispid = self._name_to_dispid_[string.lower(dispid)]
  269.             except KeyError:
  270.                 raise COMException(scode = winerror.DISP_E_MEMBERNOTFOUND, desc = 'Member not found')
  271.  
  272.         
  273.         return self._invoke_(dispid, lcid, wFlags, args)
  274.  
  275.     
  276.     def _invoke_(self, dispid, lcid, wFlags, args):
  277.         return (S_OK, -1, self._invokeex_(dispid, lcid, wFlags, args, None, None))
  278.  
  279.     
  280.     def _GetIDsOfNames_(self, names, lcid):
  281.         '''The main COM entry-point for GetIDsOfNames.
  282.  
  283.        This checks the validity of the arguments, and calls the _getidsofnames_ helper.
  284.     '''
  285.         if len(names) > 1:
  286.             raise COMException(scode = winerror.DISP_E_INVALID, desc = 'Cannot support member argument names')
  287.         
  288.         return self._getidsofnames_(names, lcid)
  289.  
  290.     
  291.     def _getidsofnames_(self, names, lcid):
  292.         return (self._getdispid_(names[0], 0),)
  293.  
  294.     
  295.     def _GetDispID_(self, name, fdex):
  296.         return self._getdispid_(name, fdex)
  297.  
  298.     
  299.     def _getdispid_(self, name, fdex):
  300.         
  301.         try:
  302.             return self._name_to_dispid_[string.lower(str(name))]
  303.         except KeyError:
  304.             raise COMException(scode = winerror.DISP_E_UNKNOWNNAME)
  305.  
  306.  
  307.     
  308.     def _InvokeEx_(self, dispid, lcid, wFlags, args, kwargs, serviceProvider):
  309.         '''The main COM entry-point for InvokeEx.  
  310.  
  311.        This calls the _invokeex_ helper.
  312.     '''
  313.         if type(dispid) == type(''):
  314.             
  315.             try:
  316.                 dispid = self._name_to_dispid_[string.lower(dispid)]
  317.             except KeyError:
  318.                 raise COMException(scode = winerror.DISP_E_MEMBERNOTFOUND, desc = 'Member not found')
  319.  
  320.         
  321.         return self._invokeex_(dispid, lcid, wFlags, args, kwargs, serviceProvider)
  322.  
  323.     
  324.     def _invokeex_(self, dispid, lcid, wFlags, args, kwargs, serviceProvider):
  325.         '''A stub for _invokeex_ - should never be called.  
  326.  
  327.        Simply raises an exception.
  328.     '''
  329.         raise error, 'This class does not provide _invokeex_ semantics'
  330.  
  331.     
  332.     def _DeleteMemberByName_(self, name, fdex):
  333.         return self._deletememberbyname_(name, fdex)
  334.  
  335.     
  336.     def _deletememberbyname_(self, name, fdex):
  337.         raise COMException(scode = winerror.E_NOTIMPL)
  338.  
  339.     
  340.     def _DeleteMemberByDispID_(self, id):
  341.         return self._deletememberbydispid(id)
  342.  
  343.     
  344.     def _deletememberbydispid_(self, id):
  345.         raise COMException(scode = winerror.E_NOTIMPL)
  346.  
  347.     
  348.     def _GetMemberProperties_(self, id, fdex):
  349.         return self._getmemberproperties_(id, fdex)
  350.  
  351.     
  352.     def _getmemberproperties_(self, id, fdex):
  353.         raise COMException(scode = winerror.E_NOTIMPL)
  354.  
  355.     
  356.     def _GetMemberName_(self, dispid):
  357.         return self._getmembername_(dispid)
  358.  
  359.     
  360.     def _getmembername_(self, dispid):
  361.         raise COMException(scode = winerror.E_NOTIMPL)
  362.  
  363.     
  364.     def _GetNextDispID_(self, fdex, dispid):
  365.         return self._getnextdispid_(fdex, dispid)
  366.  
  367.     
  368.     def _getnextdispid_(self, fdex, dispid):
  369.         ids = self._name_to_dispid_.values()
  370.         ids.sort()
  371.         if DISPID_STARTENUM in ids:
  372.             ids.remove(DISPID_STARTENUM)
  373.         
  374.         if dispid == DISPID_STARTENUM:
  375.             return ids[0]
  376.         else:
  377.             
  378.             try:
  379.                 return ids[ids.index(dispid) + 1]
  380.             except ValueError:
  381.                 raise COMException(scode = winerror.E_UNEXPECTED)
  382.             except IndexError:
  383.                 raise COMException(scode = winerror.S_FALSE)
  384.  
  385.  
  386.     
  387.     def _GetNameSpaceParent_(self):
  388.         return self._getnamespaceparent()
  389.  
  390.     
  391.     def _getnamespaceparent_(self):
  392.         raise COMException(scode = winerror.E_NOTIMPL)
  393.  
  394.  
  395.  
  396. class MappedWrapPolicy(BasicWrapPolicy):
  397.     '''Wraps an object using maps to do its magic
  398.  
  399.      This policy wraps up a Python object, using a number of maps
  400.      which translate from a Dispatch ID and flags, into an object to call/getattr, etc.
  401.  
  402.      It is the responsibility of derived classes to determine exactly how the
  403.      maps are filled (ie, the derived classes determine the map filling policy.
  404.  
  405.      This policy supports the following special attributes on the wrapped object
  406.  
  407.      _dispid_to_func_/_dispid_to_get_/_dispid_to_put_ -- These are dictionaries
  408.        (keyed by integer dispid, values are string attribute names) which the COM
  409.        implementation uses when it is processing COM requests.  Note that the implementation
  410.        uses this dictionary for its own purposes - not a copy - which means the contents of 
  411.        these dictionaries will change as the object is used.
  412.  
  413.   '''
  414.     
  415.     def _wrap_(self, object):
  416.         BasicWrapPolicy._wrap_(self, object)
  417.         ob = self._obj_
  418.         if hasattr(ob, '_dispid_to_func_'):
  419.             self._dispid_to_func_ = ob._dispid_to_func_
  420.         else:
  421.             self._dispid_to_func_ = { }
  422.         if hasattr(ob, '_dispid_to_get_'):
  423.             self._dispid_to_get_ = ob._dispid_to_get_
  424.         else:
  425.             self._dispid_to_get_ = { }
  426.         if hasattr(ob, '_dispid_to_put_'):
  427.             self._dispid_to_put_ = ob._dispid_to_put_
  428.         else:
  429.             self._dispid_to_put_ = { }
  430.  
  431.     
  432.     def _getmembername_(self, dispid):
  433.         if self._dispid_to_func_.has_key(dispid):
  434.             return self._dispid_to_func_[dispid]
  435.         elif self._dispid_to_get_.has_key(dispid):
  436.             return self._dispid_to_get_[dispid]
  437.         elif self._dispid_to_put_.has_key(dispid):
  438.             return self._dispid_to_put_[dispid]
  439.         else:
  440.             raise COMException(scode = winerror.DISP_E_MEMBERNOTFOUND)
  441.  
  442.  
  443.  
  444. class DesignatedWrapPolicy(MappedWrapPolicy):
  445.     '''A policy which uses a mapping to link functions and dispid
  446.      
  447.      A MappedWrappedPolicy which allows the wrapped object to specify, via certain
  448.      special named attributes, exactly which methods and properties are exposed.
  449.  
  450.      All a wrapped object need do is provide the special attributes, and the policy
  451.      will handle everything else.
  452.  
  453.      Attributes:
  454.  
  455.      _public_methods_ -- Required -- A list of strings, which must be the names of 
  456.                   methods the object provides.  These methods will be exposed and 
  457.                   callable from other COM hosts.
  458.      _public_attrs_ A list of strings, which must be the names of attributes on the object.
  459.                   These attributes will be exposed and readable and possibly writeable from other COM hosts.
  460.      _readonly_attrs_ -- A list of strings, which must also appear in _public_attrs.  These
  461.                   attributes will be readable, but not writable, by other COM hosts.
  462.      _value_ -- A method that will be called if the COM host requests the "default" method
  463.                   (ie, calls Invoke with dispid==DISPID_VALUE)
  464.      _NewEnum -- A method that will be called if the COM host requests an enumerator on the
  465.                   object (ie, calls Invoke with dispid==DISPID_NEWENUM.)
  466.                   It is the responsibility of the method to ensure the returned
  467.                   object conforms to the required Enum interface.
  468.                   
  469.      _Evaluate -- Dunno what this means, except the host has called Invoke with dispid==DISPID_EVALUATE!
  470.                   See the COM documentation for details.
  471.   '''
  472.     
  473.     def _wrap_(self, ob):
  474.         MappedWrapPolicy._wrap_(self, ob)
  475.         if not hasattr(ob, '_public_methods_'):
  476.             raise error, 'Object does not support DesignatedWrapPolicy, as it does not have an _public_methods_ attribute.'
  477.         
  478.         for dispid, name in self._dispid_to_func_.items():
  479.             self._name_to_dispid_[string.lower(name)] = dispid
  480.         
  481.         for dispid, name in self._dispid_to_get_.items():
  482.             self._name_to_dispid_[string.lower(name)] = dispid
  483.         
  484.         for dispid, name in self._dispid_to_put_.items():
  485.             self._name_to_dispid_[string.lower(name)] = dispid
  486.         
  487.         if hasattr(ob, '_value_'):
  488.             self._dispid_to_get_[DISPID_VALUE] = '_value_'
  489.             self._dispid_to_put_[DISPID_PROPERTYPUT] = '_value_'
  490.         
  491.         if hasattr(ob, '_NewEnum'):
  492.             self._name_to_dispid_['_newenum'] = DISPID_NEWENUM
  493.             self._dispid_to_func_[DISPID_NEWENUM] = '_NewEnum'
  494.         
  495.         if hasattr(ob, '_Evaluate'):
  496.             self._name_to_dispid_['_evaluate'] = DISPID_EVALUATE
  497.             self._dispid_to_func_[DISPID_EVALUATE] = '_Evaluate'
  498.         
  499.         dispid = 1000
  500.         if hasattr(ob, '_public_attrs_'):
  501.             if hasattr(ob, '_readonly_attrs_'):
  502.                 readonly = ob._readonly_attrs_
  503.             else:
  504.                 readonly = []
  505.             for name in ob._public_attrs_:
  506.                 self._name_to_dispid_[string.lower(name)] = dispid
  507.                 self._dispid_to_get_[dispid] = name
  508.                 if name not in readonly:
  509.                     self._dispid_to_put_[dispid] = name
  510.                 
  511.                 dispid = dispid + 1
  512.             
  513.         
  514.         for name in ob._public_methods_:
  515.             self._name_to_dispid_[string.lower(name)] = dispid
  516.             self._dispid_to_func_[dispid] = name
  517.             dispid = dispid + 1
  518.         
  519.  
  520.     
  521.     def _invokeex_(self, dispid, lcid, wFlags, args, kwArgs, serviceProvider):
  522.         if wFlags & DISPATCH_METHOD:
  523.             
  524.             try:
  525.                 funcname = self._dispid_to_func_[dispid]
  526.             except KeyError:
  527.                 if not (wFlags & DISPATCH_PROPERTYGET):
  528.                     raise COMException(scode = winerror.DISP_E_MEMBERNOTFOUND)
  529.                 
  530.             except:
  531.                 not (wFlags & DISPATCH_PROPERTYGET)
  532.  
  533.             
  534.             try:
  535.                 func = getattr(self._obj_, funcname)
  536.             except AttributeError:
  537.                 raise COMException(scode = winerror.DISP_E_MEMBERNOTFOUND)
  538.  
  539.             return apply(func, args)
  540.         
  541.         if wFlags & DISPATCH_PROPERTYGET:
  542.             
  543.             try:
  544.                 name = self._dispid_to_get_[dispid]
  545.             except KeyError:
  546.                 raise COMException(scode = winerror.DISP_E_MEMBERNOTFOUND)
  547.  
  548.             retob = getattr(self._obj_, name)
  549.             if type(retob) == types.MethodType:
  550.                 retob = apply(retob, args)
  551.             
  552.             return retob
  553.         
  554.         if wFlags & (DISPATCH_PROPERTYPUT | DISPATCH_PROPERTYPUTREF):
  555.             
  556.             try:
  557.                 name = self._dispid_to_put_[dispid]
  558.             except KeyError:
  559.                 raise COMException(scode = winerror.DISP_E_MEMBERNOTFOUND)
  560.  
  561.             setattr(self._obj_, name, args[0])
  562.             return None
  563.         
  564.         raise COMException(scode = winerror.E_INVALIDARG, desc = 'invalid wFlags')
  565.  
  566.  
  567.  
  568. class EventHandlerPolicy(DesignatedWrapPolicy):
  569.     '''The default policy used by event handlers in the win32com.client package.
  570.  
  571.     In addition to the base policy, this provides argument conversion semantics for
  572.     params
  573.       * dispatch params are converted to dispatch objects.
  574.       * Unicode objects are converted to strings (1.5.2 and earlier)
  575.  
  576.     NOTE: Later, we may allow the object to override this process??
  577.     '''
  578.     
  579.     def _transform_args_(self, args, kwArgs, dispid, lcid, wFlags, serviceProvider):
  580.         ret = []
  581.         for arg in args:
  582.             if type(arg) == IDispatchType:
  583.                 import win32com.client as win32com
  584.                 arg = win32com.client.Dispatch(arg)
  585.             elif not core_has_unicode and type(arg) == UnicodeType:
  586.                 arg = str(arg)
  587.             
  588.             ret.append(arg)
  589.         
  590.         return (tuple(ret), kwArgs)
  591.  
  592.     
  593.     def _invokeex_(self, dispid, lcid, wFlags, args, kwArgs, serviceProvider):
  594.         (args, kwArgs) = self._transform_args_(args, kwArgs, dispid, lcid, wFlags, serviceProvider)
  595.         return DesignatedWrapPolicy._invokeex_(self, dispid, lcid, wFlags, args, kwArgs, serviceProvider)
  596.  
  597.  
  598.  
  599. class DynamicPolicy(BasicWrapPolicy):
  600.     """A policy which dynamically (ie, at run-time) determines public interfaces.
  601.   
  602.      A dynamic policy is used to dynamically dispatch methods and properties to the
  603.      wrapped object.  The list of objects and properties does not need to be known in
  604.      advance, and methods or properties added to the wrapped object after construction
  605.      are also handled.
  606.  
  607.      The wrapped object must provide the following attributes:
  608.  
  609.      _dynamic_ -- A method that will be called whenever an invoke on the object
  610.             is called.  The method is called with the name of the underlying method/property
  611.             (ie, the mapping of dispid to/from name has been resolved.)  This name property
  612.             may also be '_value_' to indicate the default, and '_NewEnum' to indicate a new
  613.             enumerator is requested.
  614.             
  615.   """
  616.     
  617.     def _wrap_(self, object):
  618.         BasicWrapPolicy._wrap_(self, object)
  619.         if not hasattr(self._obj_, '_dynamic_'):
  620.             raise error, 'Object does not support Dynamic COM Policy'
  621.         
  622.         self._next_dynamic_ = self._min_dynamic_ = 1000
  623.         self._dyn_dispid_to_name_ = {
  624.             DISPID_VALUE: '_value_',
  625.             DISPID_NEWENUM: '_NewEnum' }
  626.  
  627.     
  628.     def _getdispid_(self, name, fdex):
  629.         lname = string.lower(str(name))
  630.         
  631.         try:
  632.             return self._name_to_dispid_[lname]
  633.         except KeyError:
  634.             dispid = self._next_dynamic_ = self._next_dynamic_ + 1
  635.             self._name_to_dispid_[lname] = dispid
  636.             self._dyn_dispid_to_name_[dispid] = name
  637.             return dispid
  638.  
  639.  
  640.     
  641.     def _invoke_(self, dispid, lcid, wFlags, args):
  642.         return (S_OK, -1, self._invokeex_(dispid, lcid, wFlags, args, None, None))
  643.  
  644.     
  645.     def _invokeex_(self, dispid, lcid, wFlags, args, kwargs, serviceProvider):
  646.         
  647.         try:
  648.             name = str(self._dyn_dispid_to_name_[dispid])
  649.         except KeyError:
  650.             raise COMException(scode = winerror.DISP_E_MEMBERNOTFOUND, desc = 'Member not found')
  651.  
  652.         return self._obj_._dynamic_(name, lcid, wFlags, args)
  653.  
  654.  
  655. DefaultPolicy = DesignatedWrapPolicy
  656.  
  657. def resolve_func(spec):
  658.     """Resolve a function by name
  659.   
  660.   Given a function specified by 'module.function', return a callable object
  661.   (ie, the function itself)
  662.   """
  663.     
  664.     try:
  665.         idx = string.rindex(spec, '.')
  666.         mname = spec[:idx]
  667.         fname = spec[idx + 1:]
  668.         module = _import_module(mname)
  669.         return getattr(module, fname)
  670.     except ValueError:
  671.         return globals()[spec]
  672.  
  673.  
  674.  
  675. def call_func(spec, *args):
  676.     """Call a function specified by name.
  677.   
  678.   Call a function specified by 'module.function' and return the result.
  679.   """
  680.     return apply(resolve_func(spec), args)
  681.  
  682.  
  683. def _import_module(mname):
  684.     """Import a module just like the 'import' statement.
  685.  
  686.   Having this function is much nicer for importing arbitrary modules than
  687.   using the 'exec' keyword.  It is more efficient and obvious to the reader.
  688.   """
  689.     __import__(mname)
  690.     return sys.modules[mname]
  691.  
  692.  
  693. try:
  694.     from dispatcher import DispatcherTrace, DispatcherWin32trace
  695. except ImportError:
  696.     pass
  697.  
  698.